<?php

class Language_editor extends Controller {

	function Language_editor()
	{
		parent::Controller();
		$this->load->library('i18n_manager');

		$this->load->helper('url');
	}

	function index()
	{
		if($this->session->userdata('i18n_manager')!==FALSE){
			$this->menu();
		}else{
			$this->load->view('/digici/i18n_manager/html', array('html' => $this->load->view('/digici/i18n_manager/login',array(),TRUE)));
		}
	}

	function login(){
		$username = $this->input->get_post('username');
		$password = $this->input->get_post('password');

		$this->config->load('digici/i18n_manager');
		$login = $this->config->item('i18n_users');

		$this->session->unset_userdata('i18n_langsenabled');

		foreach($login as $user=>$userdata){
			if (($user == $username ) && ($userdata['password'] == $password )){
				$this->_start_translation();

				$i18n_session = array('i18n_langsenabled' => $userdata['langs']);
				if (isset($userdata['admin']) && $userdata['admin']){
					$i18n_session['i18n_admin'] = TRUE;
				}
				$this->session->set_userdata( $i18n_session );
			}
		}
		redirect('digici/language_editor');
	}

	function logout(){
		$this->_end_translation();
		redirect('digici/language_editor');
	}

	function _start_translation(){
		$this->session->set_userdata('i18n_manager', array(''));
	}

	function _end_translation(){
		$this->session->unset_userdata('i18n_manager', array(''));
	}

	function _check_langenabled($lang){
		if($lang=='' && $this->session->userdata('i18n_manager')!==FALSE){
			return TRUE;
		}else{
			if($lang==''){
				redirect('digici/language_editor');
			}
		}

		if ( in_array($lang, $this->session->userdata('i18n_langsenabled')) ){
			return TRUE;
		}else{
			return FALSE;
		}
	}

	function menu($language	= '', $file = '')
	{
		if($this->_check_langenabled($language)){
			$html	= $this->_show_banner($language, $file);

			if($language)
			{
				$this->session->set_userdata('lang',$language);
				$html .= $this->session->userdata('i18n_messages');
				$this->session->unset_userdata('i18n_messages');

				$html	.= $this->i18n_manager->get_list_files($language);
			}
			else
			{
				$html .= '<h2>List of languages</h2>';
				if($this->session->userdata('i18n_admin')){
					$html .= '<p><a class="newfile" href="'.site_url('digici/language_editor/new_file').'">New language file</a></p>';
				}
				$html	.= $this->i18n_manager->get_list_languages();
			}

			$this->load->view('/digici/i18n_manager/html', array('html' => $html));
		}else{
			$this->load->view('/digici/i18n_manager/html', array('html' => $this->load->view('/digici/i18n_manager/no_perm',array(),TRUE)));
		}
	}

	function remove($lang){
		if($this->session->userdata('i18n_admin')){
			if($this->input->get('confirm') == 'Y'){
				if (
				 	$this->i18n_manager->remove_files( str_replace('.html', '', $this->input->get('file')) )
				 ){
					$this->session->set_userdata('i18n_messages','<div class="ok">files removed successfully!!</div>');
				 }else{
				 	$this->session->set_userdata('i18n_messages','<div class="error">error removing some files!!</div>');
				 }
			}else{
				$this->session->set_userdata('i18n_messages','<div class="error">confirm first!!!</div>');
			}
		}

		redirect(site_url('digici/language_editor/menu/'.$lang),'location');
	}

	function new_file(){
		if($this->session->userdata('i18n_admin')){
			// load required helper
			$this->load->library('table');
			$this->load->helper('form');

			// set hidden fields
			$this->config->load('digici/18n_manager');
			$available_langs = $this->config->item('available_languages');
			$hidden_fields	= array(
				'language'	=> $available_langs[0]
			);

			$content = array('KEY'=>'VALUE');
			// set table header
			$this->table->set_heading('KEY INDEX', 'TEXT VALUE');

			// transform each key into input textbox
			foreach($content as $key => $val)
			{
				$input	= array(
					'name' 		=> $key
					, 'id'		=> $key
					, 'value'	=> $val
				);
				$inputkey = array(
					'value'		=> 'KEY',
					'class'		=> 'newfield',
					'onchange'	=> 'setname(this);'
				);

				$this->table->add_row(form_input($inputkey), ( (strpos($val, '\n') !==FALSE || strlen($val)>70)?form_textarea($input):form_input($input) ) );
			}

			// submit button attributes
			$submit	= array(
				'name'		=> 'submitted'
				, 'value'	=> 'Save'
			);

			$html   = $this->session->userdata('i18n_messages');
			$this->session->unset_userdata('i18n_messages');
			$html	.= $this->_show_banner($available_langs[0]);

			// draw form
			$html.= '<div><a href="#" id="add_key">Add KEY & VALUE</a></div>';
			$html 	.= form_open($this->i18n_manager->_process_controller, array('id'=>'save_items'), $hidden_fields);
			$html   .= '<div>'.form_label('FILENAME (WITH PATH):', 'file').form_input(array('name'=>'file', 'value'=>'RELATIVEPATH/FILENAME_WHITHOUT_LANG_SUFFIX')).'</div>';
			// draw table
			$html	.= $this->table->generate();
			// draw submit button
			$html	.= '<button type="submit">Save</button> <button type="reset" id="reset_form">Reset</button>';

			// draw closing form tag
			$html	.= form_close();

			$this->load->view('/digici/i18n_manager/html', array('html' => $html));
		}else{
			redirect(site_url('digici/language_editor'));
		}
	}

	function edit($language, $file='')
	{
		if($this->_check_langenabled($language)){
			if ($this->input->get('file')){
				$file = str_replace('.html','', $this->input->get('file'));
			}

			// load required helper
			$this->load->library('table');
			$this->load->helper('form');

			// get the content of the file we edit
			$content	= $this->i18n_manager->get_language_file_content($language, $file . '_lang.php');

			// set hidden fields
			$hidden_fields	= array(
				'language'	=> $language
				, 'file'	=> $file
			);

			// set table header
			$this->table->set_heading('KEY INDEX', 'TEXT VALUE');

			// transform each key into input textbox
			foreach($content as $key => $val)
			{
				$input	= array(
					'name' 		=> $key
					, 'id'		=> $key
					, 'value'	=> $val
				);

				$this->table->add_row(
					form_label($key, $key),
					( (strpos($val, '\n') !==FALSE || strlen($val)>70)?form_textarea($input):form_input($input) ).
					''  //añadir enlaces para gtranslator http://www.greywyvern.com/code/php/binary2base64
					);
			}

			// submit button attributes
			$submit	= array(
				'name'		=> 'submitted'
				, 'value'	=> 'Save'
			);

			$html   = $this->session->userdata('i18n_messages');
			$this->session->unset_userdata('i18n_messages');
			$html	.= $this->_show_banner($language, $file);
			// draw form
			$html.= '<div><a href="#" id="add_key">Add KEY & VALUE</a></div>';
			$html 	.= form_open($this->i18n_manager->_process_controller, array('id'=>'save_items'), $hidden_fields);
			// draw table
			$html	.= $this->table->generate();
			// draw submit button
			$html	.= '<button type="submit">Save</button> <button type="reset" id="reset_form">Reset</button>';

			// draw closing form tag
			$html	.= form_close();

			$this->load->view('/digici/i18n_manager/html', array('html' => $html));
		}else{
			$this->load->view('/digici/i18n_manager/html', array('html' => $this->load->view('/digici/i18n_manager/no_perm',array(),TRUE)));
		}
	}


	function eipsave()
	{
		if($this->_check_langenabled($language)){
			$json_err	= array();

			$language	= $this->input->post('language');
			$line		= $this->input->post('line', TRUE);
			$text 		= $this->input->post('value', TRUE);

			if($language && $line && $text)
			{
				if((list($file, $content) = $this->i18n_manager->find_file($language, $line)) !== false)
				{
					$content[$line]	= $text;

					if($this->i18n_manager->put_content($language, $file, $content))
					{
						$json_err	= array(
							'errorCode'	=> 0
							, 'value'	=> $text
						);
					}
					else
					{
						$json_err	= array(
							'errorCode'	=> 1
							, 'errorMsg'	=> 'error: cannot save'
						);
					}
				}
				else
				{
					$json_err	= array(
						'errorCode'	=> 1
						, 'errorMsg'	=> 'error: file not found'
					);
				}
			}
			else
			{
				$json_err	= array(
					'errorCode'	=> 1
					, 'errorMsg'	=> 'error: info missing'
				);
			}

			exit(json_encode($json_err));
		}else{
			$this->load->view('/digici/i18n_manager/html', array('html' => $this->load->view('/digici/i18n_manager/no_perm',array(),TRUE)));
		}
	}


	function save()
	{
		if($this->_check_langenabled($this->input->post('language', ''))){

			$language	= $this->input->post('language', TRUE);
			$file		= $this->input->post('file', TRUE);

			foreach($_POST as $field => $value)
			{
				if(in_array($field, array('language', 'file', 'submitted')))
					continue;
				$array[$field]	= $this->input->post($field, TRUE);
			}

			if($this->i18n_manager->put_content($language, $file, $array))
			{
				$this->session->set_userdata('i18n_messages','<div class="ok">File saved successfully!</div>');
			}else{
				$this->session->set_userdata('i18n_messages','<div class="error">Error saving the file!</div>');
			}

			if($this->input->server('HTTP_REFERER')!==FALSE){
				redirect($this->input->server('HTTP_REFERER'));
			}else{
				redirect(site_url('digici/language_editor'));
			}
		}else{
			$this->load->view('/digici/i18n_manager/html', array('html' => $this->load->view('/digici/i18n_manager/no_perm',array(),TRUE)));
		}
	}


	function _show_banner($language = "", $file = "")
	{
		$link 	= '';
		$html	= '';
		if($language)
		{
			$html	= "<h2>Editing language: " . $language . "</h2>";
			$link	= "<a href=\"" . site_url($this->i18n_manager->_base_controller) . "\">Back to language list</a><br />";

		}
		if($file)
		{
			$html	.= "<h1>Editing language: " . $this->i18n_manager->_files[$language][$file . '_lang.php'] . "</h1>";
			$link	.= "<a href=\"" . site_url($this->i18n_manager->_menu_controller . $language) . "\">Back to File list</a><br />";
		}

		return $html . $link . '<br />';

	}
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/editor.php */